<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">用户信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户信息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="200" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="nickname" label="昵称" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="phone" label="电话" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="identityNum" label="身份证号" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="address" label="地址" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="role" label="角色" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="avatar" label="头像" width="100" align="center"></el-table-column>
      <el-table-column label="是否启用" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 以下是修改相册的弹出表单Dialog -->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
      <el-form-item label="用户名">
        <el-input v-model="editForm.username"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="editForm.nickname"></el-input>
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="editForm.phone"></el-input>
      </el-form-item>

      <el-form-item label="身份证号">
        <el-input v-model="editForm.identityNum"></el-input>
      </el-form-item>
      <el-form-item label="家庭住址">
        <el-input v-model="editForm.address"></el-input>
      </el-form-item>

      <el-form-item label="角色">
        <template>
          <el-select v-model="editForm.role" clearable placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      // currentParentId: 0,
      // history: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      options: [{
        value: '0',
        label: '普通用户'
      }, {
        value: '1',
        label: '管理员'
      }, ],
      editForm: {
        username: '',
        nickname: '',
        phone: '',
        identityNum: '',
        address: '',
        role: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    //启用禁用
    changeEnable(user) {
      console.log(user)
      let enableText = ['禁用', '启用'];
      let url = 'http://120.77.154.166:8080/user/' + user.id;
      let enable = user.enable;
      if (enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: enableText[enable] + '【' + user.username + '】状态修改成功！',
            type: 'success'
          });
          this.loadBrandList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadBrandList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    // goBack() {
    //  let  parentCategory= this.history[this.history.length - 1];
    //   this.history.pop();//删除history数组的最后一个数据
    //   this.currentParentId = parentCategory.parentId;
    //   this.loadBrandList();
    // },
    // showSubCategoryList(user) {
    //   this.history[user.depth-1]=user
    //   this.currentParentId = user.id;
    //   this.loadBrandList();
    // },

    //修改
    showEditFormDialog(user) {
      this.editForm = user;
      this.dialogFormVisible = true;
      console.log(user)

      let url = 'http://120.77.154.166:8080/user/' + user.id+'/selectById';;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadBrandList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://120.77.154.166:8080/user/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改用户成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadBrandList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadBrandList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    openDeleteConfirm(user) {
      let message = '此操作将永久删除【' + user.username + '】用户，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(user);
      }).catch(() => {
      });
    },
    handleDelete(user) {
      let url = 'http://120.77.154.166:8080/user/' + user.id + '/deleteById';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' +user.username + '】用户成功！',
            type: 'success'
          });
          this.loadBrandList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadBrandList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    loadBrandList() {
      let url = 'http://120.77.154.166:8080/user/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          console.log(this.tableData)
        }
      });
    }
  },
  mounted() {
    this.loadBrandList();
  }
}
</script>
